<script lang="ts" setup>
import { ref, reactive, toRefs } from "vue" // vue
import { FormInstance, ElMessageBox, ElMessage } from "element-plus" // elment
// import { Search, Refresh } from "@element-plus/icons-vue" // 按钮图标
import {
  getclassificationDataApi,
  createDataApi,
  putDataApi,
  delDataApi
} from "@/api/repository/classificationRepository" // 接口
import { useUserStore } from "@/store/modules/user" // 获取用户信息

const userStore = useUserStore() // 获取用户信息

// 表单
const data = reactive<any>({
  formData: {},
  searchData: {
    name: undefined
  }, // 表单验证
  rules: {
    name: [
      {
        required: true,
        message: "请输入一级分类",
        trigger: "blur"
      }
    ]
  }
})
const { searchData, formData, rules } = toRefs(data)

// #region 查
const tableData = ref<any>([])
function tablelist() {
  getclassificationDataApi(searchData.value)
    .then((res: any) => {
      tableData.value = res.data
      // console.log(tableData.value)
      // console.log(tableDataSubs.value)
    })
    .catch(() => {
      tableData.value = []
    })
}
tablelist()
// #endregion

// #region 搜索内容
// const searchFormRef = ref<FormInstance | null>(null)
// 搜索
// function handleSearch() {
//   tablelist()
// }
// 重置
// function resetSearch() {
//   searchFormRef.value?.resetFields()
//   handleSearch()
// }
// #endregion

// #region 添加和修改框
const dialogVisible = ref(false)
const formRef = ref<FormInstance | null>(null)
const ids = ref(undefined) // 判断修改还是增加

// 修改
const handleUpdate = (row: any) => {
  console.log(row, 88)

  ids.value = row.id
  dialogVisible.value = true
  formData.value = JSON.parse(JSON.stringify(row)) // 赋值需要深拷贝
}

// 删除
const handleDelete = (row: any) => {
  ElMessageBox.confirm(`您确定要删除吗?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    delDataApi({ id: row.id }).then(() => {
      ElMessage.success("删除成功")
      tablelist()
    })
    console.log(row, 3)
  })
}

// 清空form框
const clearValidate = () => {
  formRef.value?.resetFields()
}

// 清空对话框
const resetForm = () => {
  ids.value = undefined
  clearValidate()
  formData.value = {}
}

// 确定按钮
const handleCreate = () => {
  formRef.value?.validate((valid: boolean) => {
    if (valid) {
      if (ids.value === undefined) {
        createDataApi(formData.value).then(() => {
          ElMessage.success("新增成功")
          dialogVisible.value = false
          tablelist()
        })
      } else {
        putDataApi(formData.value).then(() => {
          ElMessage.success("修改成功")
          dialogVisible.value = false
          tablelist()
        })
      }
    } else {
      return false
    }
  })
}
// #endregion
</script>

<template>
  <div class="app-container">
    <!-- <el-card>
      <el-row justify="center" align="middle">
        <el-col :span="14">
          <el-form ref="searchFormRef" :inline="true" :model="searchData">
            <el-form-item prop="name" label="分类名称">
              <el-input v-model.trim="searchData.name" clearable placeholder="请输入" style="width: 500px" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
              <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
              <el-button @click="dialogVisible = true">新增分类</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-card> -->

    <el-card style="margin-top: 20px">
      <el-button
        type="primary"
        style="margin-bottom: 20px"
        @click="dialogVisible = true"
        v-if="
          userStore.roles.join() == '超级管理员' ||
          userStore.roles.join() == '质检部客服组' ||
          userStore.roles.join() == '子账户超级管理员' ||
          userStore.roles.join() == '渠道部子账户管理员'
        "
        >新增分类</el-button
      >
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column prop="name" label="一级分类" />
        <el-table-column
          fixed="right"
          label="操作"
          width="150"
          align="center"
          v-if="
            userStore.roles.join() == '超级管理员' ||
            userStore.roles.join() == '质检部客服组' ||
            userStore.roles.join() == '子账户超级管理员' ||
            userStore.roles.join() == '渠道部子账户管理员'
          "
        >
          <template #default="scope">
            <el-button type="primary" text bg size="small" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button type="danger" text bg size="small" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- <el-collapse>
        <el-collapse-item v-for="(item, index) in tableData" :key="index" :name="item.id">
          <template #title>
            <div style="margin-right: 50px">
              <span>一级分类： {{ item.name }}</span>
            </div>
            <div style="margin-right: 50px">
              <span v-if="item.type == 'job'">类型： 岗位知识库</span>
              <span v-if="item.type == 'comprehensive'">类型： 综合知识库</span>
            </div>
            <el-button type="primary" text bg size="small" @click="handleUpdate(item)">修改</el-button>
            <el-button type="danger" text bg size="small" @click="handleDelete(item)">删除</el-button>
          </template>
          <div
            v-for="(i, d) in item.subs"
            :key="d"
            style="display: flex; justify-content: space-between; margin-top: 10px"
          >
            <div style="margin: 0 0 10px 50px">分类名称: {{ i.name }}</div>
            <div>
              <el-button type="danger" text bg size="small" @click="handleDelete(i)">删除</el-button>
            </div>
          </div>
        </el-collapse-item>
      </el-collapse> -->
    </el-card>

    <el-dialog v-model="dialogVisible" @closed="resetForm" width="60%">
      <el-form ref="formRef" :model="formData" label-width="100px" label-position="left" :rules="rules">
        <el-form-item label="类型">
          <el-select v-model="formData.type" filterable clearable style="width: 240px">
            <el-option key="1" label="岗位知识库" value="job" />
            <el-option key="2" label="综合知识库" value="comprehensive" />
            <el-option key="3" label="奖惩中心" value="rewardsORpunishments" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="一级分类">
          <el-select v-model="formData.parentId" filterable clearable style="width: 240px">
            <el-option v-for="item in tableData" :key="item.id" :label="item.name" :value="item.id"
          /></el-select>
        </el-form-item> -->

        <el-form-item prop="name" label="一级分类">
          <el-input v-model="formData.name" placeholder="请输入" />
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreate">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.expandBox {
  max-height: 80px;
  min-height: 80px;
  overflow: hidden;
  transition: max-height ease-in-out 0.2s;
}
.expandBox2 {
  min-height: 80px;
  overflow: hidden;
  transition: max-height ease-in-out 0.2s;
}
.footbox {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
}
.pagination {
  display: flex;
  justify-content: flex-end;
}
.spans {
  margin-left: 20px;
}
</style>
